package com.zz.springboot.manager.impl;

import com.zz.springboot.dto.RoleDto;
import com.zz.springboot.dto.UserDto;
import com.zz.springboot.dto.UserRoleDto;
import com.zz.springboot.dto.UserValidDaysDto;
import com.zz.springboot.exception.BizException;
import com.zz.springboot.manager.UserManager;
import com.zz.springboot.mapper.SysRoleMapper;
import com.zz.springboot.mapper.SysUserMapper;
import com.zz.springboot.mapper.SysUserRoleMapper;
import com.zz.springboot.mapper.UserValidDaysMapper;
import com.zz.springboot.resp.enums.RespCodeEnum;
import com.zz.springboot.util.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserManagerImpl implements UserManager {

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    UserValidDaysMapper userValidDaysMapper;

    @Override
    public boolean verifyOnlyUsername(String username) {
        UserDto userDto = sysUserMapper.verifyOnlyUsername(username);
        if (userDto == null) {
            return true;
        }
        return false;
    }

    @Override
    public void insertUserData(Long userId, String username, String encodePassword) {

        UserDto userDto = new UserDto(userId, username, encodePassword);
        Integer res = sysUserMapper.insertUserData(userDto);

        if (res == null || res == 0) {
            log.error("注册用户[{}]失败", username);
            throw new BizException(RespCodeEnum.REGISTER_USER_FAIL);
        }

    }

    @Override
    public void insertUserValidDays(String username, Integer validDays) {
        UserValidDaysDto dto = new UserValidDaysDto(username, validDays);
        Integer res = userValidDaysMapper.insertUserValidDays(dto);
        if (res == 0){
            log.error("插入用户[{}]有效天数[{}]失败", username, validDays);
            throw new BizException(RespCodeEnum.REGISTER_USER_FAIL);
        }
    }

    @Override
    public void updateUserValidDays(String username, Integer validDays) {
        UserValidDaysDto dto = new UserValidDaysDto(username, validDays);
        Integer res = userValidDaysMapper.updateUserValidDays(dto);
        if (res == 0){
            log.error("更新用户[{}]有效天数[{}]失败", username, validDays);
            throw new BizException(RespCodeEnum.UPDATE_VALID_DAYS_FAIL);
        }
    }

    @Override
    public Integer selectUserValidDaysByUsername(String username) {
        return userValidDaysMapper.selectUserValidDaysByUsername(username);
    }


    @Override
    public void insertRoleData(String roleCode, String roleName) {
        RoleDto roleDto = new RoleDto(roleCode, roleName);
        Integer i = sysRoleMapper.insertRoleData(roleDto);
        if (i == 0) {
            log.error("分配权限[{}]失败", roleName);
            throw new BizException(RespCodeEnum.ASSIGN_ROLE_FAIL);
        }
    }

    @Override
    public void insertPermissionData(String permName, String description) {

    }

    @Override
    public void insertUserRoleData(Long userId, Integer roleId) {
        UserRoleDto userRoleDto = new UserRoleDto(userId, roleId);
        sysUserRoleMapper.insertUserRoleData(userRoleDto);
    }

    @Override
    public void insertRolePermissionData(Integer roleId, Integer permId) {

    }

    @Override
    public UserDto selectUserByUsername(String username) {
        return sysUserMapper.selectUserByUsername(username);
    }

    @Override
    public RoleDto selectRoleByRoleCode(String roleCode) {
        return sysRoleMapper.selectRoleByRoleCode(roleCode);
    }

    @Override
    public List<String> getRolesByUserName(String userName) {
        return sysUserMapper.getRolesByUserName(userName);
    }

    @Override
    public List<String> getPermissionsByUserName(String userName) {
        return sysUserMapper.getPermissionsByUserName(userName);
    }
}
